package api;

import org.json.JSONObject;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;

public class Sample implements APIKey {
	// 设置APPID/AK/SK
	public static final String APP_ID = "15709512";
	public static final String API_KEY = "MiqPBy2rR5NBqzMqfg4Oyqea";
	public static final String SECRET_KEY = "mQeq9RD4Tuc4xgfieMVYw2iXccpHg5bY";
	private AipContentCensor client;
	private JSONObject response;

	private Sample() {
		client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
	}

	public static Sample getInstaSample() {
		return SingleSample.mSample;
	}

	private static class SingleSample {
		private static final Sample mSample = new Sample();
	}

	public String checkPic(String imagePath) {
		// 初始化一个AipImageCensor
		// 可选：设置网络连接参数
		client.setConnectionTimeoutInMillis(2000);
		client.setSocketTimeoutInMillis(60000);
		String path = imagePath;
		try {
			response = client.imageCensorUserDefined(path, EImgType.FILE, null);
		} catch (Exception e) {
			// TODO: handle exception
			client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
			client.setConnectionTimeoutInMillis(2000);
			client.setSocketTimeoutInMillis(60000);
			response = client.imageCensorUserDefined(path, EImgType.FILE, null);
		}

		// System.out.println(response.toString());
		return response.toString();
		// 可选：设置代理服务器地址, http和socket二选一，或者均不设置
	}
}
